package cn.hnucm.orderservice.service.impl;

import cn.hnucm.orderservice.entity.TbGoodsOrder;
import cn.hnucm.orderservice.mapper.TbGoodsOrderMapper;
import cn.hnucm.orderservice.service.TbGoodsOrderService;
import cn.hnucm.vo.AddOrder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javafx.scene.input.DataFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author chenjintao
 * @since 2021-12-06
 */
@Service
public class TbGoodsOrderServiceImpl extends ServiceImpl<TbGoodsOrderMapper, TbGoodsOrder> implements TbGoodsOrderService {

    @Autowired
    private TbGoodsOrderMapper tbGoodsOrderMapper;

    /**
     * 通过订单号查询订单
     * @param orderId
     * @return
     */
    @Transactional
    @Override
    public TbGoodsOrder getOrderById(Integer orderId) {
        return tbGoodsOrderMapper.selectById(orderId);
    }

    /**
     * 关于某个商品的所有订单
     * @param goodsId
     * @return
     */
    @Transactional
    @Override
    public List<TbGoodsOrder> getAboutGoodAllOrder(Integer goodsId) {
        return getOrder(null,goodsId,null,null,null,null,null);
    }

    /**
     * 获取某个用户的所有订单
     * @param userId
     * @return
     */
    @Transactional
    @Override
    public List<TbGoodsOrder> getOneUserAllOrder(Integer userId) {
        return getOrder(userId,null,null,null,null,null,null);
    }

    /**
     * 获取某个用户卖出商品的订单
     * @param userId
     * @return
     */
    @Transactional
    @Override
    public List<TbGoodsOrder> getOneUserSellerOrder(Integer userId) {
        return getOrder(userId,null,true,null,null,null,null);
    }

    /**
     * 获取某个用户购买商品的所有订单
     * @param userId
     * @return
     */
    @Transactional
    @Override
    public List<TbGoodsOrder> getOneUserbuyOrder(Integer userId) {
        return getOrder(userId,null,null,true,null,null,null);
    }

    /**
     * 获取某个用户未支付的订单
     * @param userId
     * @return
     */
    @Transactional
    @Override
    public List<TbGoodsOrder> getNoPayOrder(Integer userId) {
        return getOrder(userId,null,null,null,1,null,null);
    }

    /**
     * 获取某个用户已经支付的订单
     * @param userId
     * @return
     */
    @Transactional
    @Override
    public List<TbGoodsOrder> getPayOrder(Integer userId) {
        return getOrder(userId,null,null,null,2,null,null);
    }

    /**
     * 获取某个用户已经送达的订单
     * @param userId
     * @return
     */
    @Transactional
    @Override
    public List<TbGoodsOrder> getSendOrder(Integer userId) {
        return getOrder(userId,null,null,null,3,null,null);
    }

    /**
     * 获取某个用户已经结束的订单
     * @param userId
     * @return
     */
    @Transactional
    @Override
    public List<TbGoodsOrder> getOverOrder(Integer userId) {
        return getOrder(userId,null,null,null,4,null,null);
    }

    /**
     * 获取某个用户某段时间的所有订单
     * @param userId
     * @param beginTime
     * @param endTime
     * @return
     */
    @Transactional
    @Override
    public List<TbGoodsOrder> getOneTimeOrder(Integer userId, String beginTime, String endTime) {
        return getOrder(userId,null,null,null,null,beginTime,endTime);
    }


    /**
     * 增加一条订单
     * @param addOrder
     * @return
     */
    @Transactional
    @Override
    public TbGoodsOrder addOrder(AddOrder addOrder) {
        TbGoodsOrder order = new TbGoodsOrder();
        order.setGoodsId(addOrder.getGoodsId());
        order.setGoodsNum(addOrder.getGoodsNum());
        order.setSellerId(addOrder.getSellerId());
        order.setBuyerId(addOrder.getBuyerId());
        order.setOrderState(2);
        order.setOrderInvoleValue(addOrder.getOrderInvoleValue());
        //设置保存的时间的格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        order.setOrderStartDatetime(sdf.format(new Date()));
//        System.out.println(tbGoodsOrderMapper.insert(order) > 0);
        if (tbGoodsOrderMapper.insert(order) > 0) {
            return order;
        }
        return null;
    }

    /**
     * 订单已完成
     * @param orderId
     * @return
     */
    @Override
    @Transactional
    public Boolean overOrder(Integer orderId) {
        TbGoodsOrder order = new TbGoodsOrder();
        order.setOrderId(orderId);
        order.setOrderState(4);
        return tbGoodsOrderMapper.updateById(order)>0;
    }

    /**
     * 通过id删除一条订单
     * @param orderId
     * @return
     */
    @Transactional
    @Override
    public Boolean deleteOrderById(Integer orderId) {
        return tbGoodsOrderMapper.deleteById(orderId)>0;
    }




    /**
     * 拼接sql语句
     * @param userId
     * @param goodsId
     * @param isSeller
     * @param isBuyer
     * @param orderState
     * @param beginTime
     * @param endTime
     * @return
     */
    private List<TbGoodsOrder> getOrder(Integer userId,Integer goodsId,Boolean isSeller,Boolean isBuyer,
                                        Integer orderState,String beginTime,String endTime){
        QueryWrapper<TbGoodsOrder> tbGoodsOrderQueryWrapper = new QueryWrapper<>();
        if (goodsId != null) tbGoodsOrderQueryWrapper.eq("goodsId",goodsId);
        if (isSeller != null && isSeller) tbGoodsOrderQueryWrapper.eq("sellerId",userId);
        if (isBuyer != null && isBuyer) tbGoodsOrderQueryWrapper.eq("buyerId",userId);
        if (orderState != null) tbGoodsOrderQueryWrapper.eq("orderState",orderState);
        if (beginTime != null && endTime != null) tbGoodsOrderQueryWrapper.between("orderStartDatetime",beginTime,endTime);
        tbGoodsOrderQueryWrapper.orderByAsc("orderStartDatetime");
        return tbGoodsOrderMapper.selectList(tbGoodsOrderQueryWrapper);
    }
}
